package com.wasu.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;

import redis.clients.jedis.exceptions.JedisConnectionException;

@ControllerAdvice
public class GlobalExceptionHandler extends BaseGlobalExceptionHandler {    

      //比如404的异常就会被这个方法捕获
      @ExceptionHandler(NoHandlerFoundException.class)    
      @ResponseStatus(HttpStatus.NOT_FOUND)    
       public ModelAndView handle404Error(HttpServletRequest req, HttpServletResponse rsp, Exception e) throws Exception {    
             return handleError(req, rsp, e, "error-front", HttpStatus.NOT_FOUND);    
       }    

      //500的异常会被这个方法捕获
      @ExceptionHandler(Exception.class)      
      @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) 
      public ModelAndView handleError(HttpServletRequest req, HttpServletResponse rsp, Exception e) throws Exception { 
             return handleError(req, rsp, e, "error-front", HttpStatus.INTERNAL_SERVER_ERROR);  
      }    

     //TODO  你也可以再写一个方法来捕获你的自定义异常
     //TRY NOW!!!

      //204的异常会被这个方法捕获
      @ExceptionHandler(JedisConnectionException.class)      
      @ResponseStatus(HttpStatus.NO_CONTENT) 
      public ModelAndView handle204Error(HttpServletRequest req, HttpServletResponse rsp, Exception e) throws Exception { 
             return handleError(req, rsp, e, "error-front", HttpStatus.NO_CONTENT);  
      } 
      
      //1000的异常会被这个方法捕获
      @ExceptionHandler(UnknownAccountException.class)      
      @ResponseStatus(HttpStatus.UNAUTHORIZED) 
      public ModelAndView handle1000Error(HttpServletRequest req, HttpServletResponse rsp, Exception e) throws Exception { 
             return handleError(req, rsp, e, "error-front", HttpStatus.UNAUTHORIZED);  
      } 
      
      //1001的异常会被这个方法捕获
      @ExceptionHandler(IncorrectCredentialsException.class)      
      @ResponseStatus(HttpStatus.UNAUTHORIZED) 
      public ModelAndView handle1001Error(HttpServletRequest req, HttpServletResponse rsp, Exception e) throws Exception { 
             return handleError(req, rsp, e, "error-front", HttpStatus.UNAUTHORIZED);  
      } 
      
      @Override    
      public Logger getLogger() {      
            return LoggerFactory.getLogger(GlobalExceptionHandler.class);    
      }

  }